package indi.h.filefilter.anaysis;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Reader;

import org.apache.log4j.Logger;
import org.apache.tika.Tika;

import indi.h.filefilter.anaysis.Anaysis.FileMessage;
import indi.h.filefilter.enums.FileEnum;
import indi.h.filefilter.enums.FilterRtEnum;
import indi.h.filefilter.filters.Filter;
import indi.h.filefilter.filters.Filter.FilterR;
import indi.h.filefilter.utils.StrFormat;

public class SimpleAnaysis implements Anaysis {
	private final static Logger l = Logger.getLogger(SimpleAnaysis.class);
	private Filter f;
	private Filter.FilterR fr;

	public SimpleAnaysis(Filter f) {
		super();
		this.f = f;
	}

	@Override
	public void setFilter(Filter f) {
		// TODO Auto-generated method stub
		this.f = f;
	}

	public static SimpleAnaysis getSimpleAnaysis(Filter f) {
		return new SimpleAnaysis(f);
	}

	@Override
	public Filter.FilterR anaysis(File fi) {
		// TODO Auto-generated method stub
		l.debug("start anaysis file");
		Tika ti = new Tika();
		FileMessage fm = new FileMessage();
		try {
			if (fi.isDirectory()) {
				return new FilterR(FilterRtEnum.PATH_DIRECTORY, "");
			} else if (pathAnaysis(fi, fm) || nameAnaysis(fi, fm) || sizeAnaysis(fi, fm) || typeAnaysis(fi, fm, ti)
					|| contentAnaysis(fi, fm, ti)) {
				return fr;
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
			return new FilterR(FilterRtEnum.IOEXCEPTION, e.getMessage());
		}
		return new FilterR(FilterRtEnum.SUCCESS, "");
	}

	public boolean pathAnaysis(File f, FileMessage m) {
		m.setTitle(FileEnum.PATH);
		return useFilter(m.setMessage(f.getAbsolutePath()));
	}

	public boolean nameAnaysis(File f, FileMessage m) {
		m.setTitle(FileEnum.NAME);
		return useFilter(m.setMessage(f.getName()));
	}

	public boolean sizeAnaysis(File f, FileMessage m) throws IOException {
		m.setTitle(FileEnum.SIZE);
		return useFilter(m.setMessage(""+ f.length()));
	}

	public boolean typeAnaysis(File f, FileMessage m, Tika t) throws IOException {
		m.setTitle(FileEnum.TYPE);
		return useFilter(m.setMessage(t.detect(f)));
	}

	public boolean contentAnaysis(File f, FileMessage m, Tika t){
		m.setTitle(FileEnum.CONTENT);
		Reader parse = null;
		BufferedReader b = null;
		try {
			parse = t.parse(f);
			b = new BufferedReader(parse);
			String line = b.readLine();
			while (line != null) {
				m.rsMessage();
				l.debug(line);
				if (!this.f.filter(m.setMessage(StrFormat.removeAllChar(line)))) {
					this.fr = this.f.filterR();
					return true;
				}
				line = b.readLine();
			}
		} catch (IOException e) {
			// TODO: handle exception
			return true;
		} finally {
			if (b != null)
				try {
					b.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if (parse != null)
				try {
					parse.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return false;
	}

	public boolean useFilter(FileMessage m) {
		if (!this.f.filter(m)) {
			this.fr = this.f.filterR();
			return true;
		}
		return false;
	}

}
